# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/1 14:40
# @Author  : wenwen
# @Site    : https://gitee.com/dimensiva/app_server.git
# @File    : my_dict.py
# @Software: PyCharm
# @Version: 3.5.4
import json


def get_value(src_dict, *key_list):
	global value
	value = src_dict
	for key in key_list:
		value = value.get(key)
		if not value:
			return None
	return value


def get_child_value(src_dict, key_list):
	global value
	value = src_dict
	for key in key_list:
		value = value.get(key)
		if not value:
			return None
	return value


def obj2dict(obj):
	"""
	对象转字典

	:param obj:
	:return:
	"""
	pr = {}
	for name in dir(obj):
		value = getattr(obj, name)
		if not callable(value) and not name.startswith('__') \
				and not name.startswith('_') \
				and name != 'query' \
				and name != 'metadata':
			pr[name] = value
	return pr


def dict2obj(para_dict):
	"""
	字典转对象

	:param para_dict:
	:return:
	"""
	top = type('new', (object,), para_dict)
	seqs = tuple, list, set, frozenset
	for i, j in para_dict.items():
		if isinstance(j, dict):
			setattr(top, i, dict2obj(j))
		elif isinstance(j, seqs):
			setattr(top, i, type(j)(dict2obj(sj) if isinstance(sj, dict) else sj for sj in j))
		else:
			setattr(top, i, j)
	return top


def dict2byte(para_dict):
	return json.dumps(para_dict, ensure_ascii=False).encode('utf-8')


def merge(dict1, dict2):
	"""
	合并字典

	:param dict1:
	:param dict2:
	:return: new_dict
	"""
	return dict(dict1, **dict2)


def change_key(src_dict, key_dict):
	"""
	改变字典的key

	:param src_dict:
	:param key_dict:
	:return: dst_dict
	"""
	dst_dict = src_dict
	for key1, key2 in key_dict.items():
		if key1 in dst_dict:
			tmp = dst_dict[key1]
			del dst_dict[key1]
			dst_dict[key2] = tmp
	return dst_dict


if __name__ == '__main__':
	aaa = {'zzz': 'fff'}
	bbb = {'ccc': 'ggg'}
	bbb['rrr'] = aaa
	print(get_value(bbb, 'rrr', 'zzz'))
# print(get_child_value(bbb, {'rrr', 'zzz'}))
# print(get_child_value(bbb, {'rrr', 'hhh'}))
# print(get_child_value(bbb, {'rrr'}))
#
# from py3_utils import my_file
#
# src_dict = my_file.load_json_file('../json/page.json')
# aaa = get_child_value(src_dict['quotationList'][0], ['voucher', 'vehicleTarget', 'vehicleLossInsuredValue'])
# print(aaa)
